<?php
namespace Components\Account\User\Wallet;

class Service {
	private $models = [];
	public function getModelsById($id = null) {
		if(is_null($id)) {
			return $this -> models;
		}

		if(!is_numeric($id) || $id <= 0) {
			return false;
		}

		if(isset($this -> models[$id]) && !empty($this -> models[$id]) && is_object($this -> models[$id])) {
			return $this -> models[$id];
		}

		$model = $this -> findFirstById($id);
		if($model['code'] != 0) {
			return false;
		}

		return $this -> models[$id] = $model['user'];
	}

	public function findFirst($parameters = []) {
		$model = Model::findFirst($parameters);
		if(!$model) {
			return ['code' => -1, 'message' => '数据不存在或已删除'];
		}

		return ['code' => 0, 'user_wallet'=>$model];
	}

	public function findFirstById($id) {
		if($id <= 0) {
			return ['code' => -1, 'message' => '数据不存在或已删除'];
		}

		$result = $this -> findFirst(['user_id = ' . $id]);
		if($result['code'] != 0) {
			return $this -> createById($id);
		}

		return $result;
	}

	public function createById($id) {
		return $this -> create([
			'user_id' => $id,
			'price_retail' => 0,
			'price_business' => 0,
			'price_shop' => 0,
			'price_trade' => 0,
			'price_activation' => 0
		]);
	}

	public function create($data) {
		$model = new Model($data);
		if(!$model -> create()) {
			$messages = $model -> getMessages();
			return ['code' => -1, 'message' => $messages[0] -> getMessage()];
		}

		return ['code' => 0, 'user_wallet'=>$model];
	}

	public function updateAmount($data) {
		if(!isset($data['user_id'])) {
			return ['code' => -1, 'message' => '用户未设置'];
		}
		if(!isset($data['amount'])) {
			return ['code' => -1, 'message' => '数量未设置'];
		}
		if(!isset($data['type'])) {
			return ['code' => -1, 'message' => '属性未设置'];
		}

		switch($data['type']) {
			case 1:
			case '1':
				$field = 'price_retail';
				break;
			case 2:
			case '2':
				$field = 'price_business';
				break;
			case 3:
			case '3':
				$field = 'price_shop';
				break;
			case 4:
			case '4':
				$field = 'price_trade';
				break;
			case 5:
			case '5':
				$field = 'price_activation';
				break;
			default:
				return ['code' => -1, 'message' => '属性设置有误'];
		}

		$result = $this -> findFirstById($data['user_id']);
		if($result['code'] != 0) {
			return $result;
		}

		$dataLog = [
			'user_id' => $data['user_id'],
			'type' => 0,
			'field' => $data['type'],
			'active' => $data['active'],
			'amount' => $data['amount'],
			'old_amount' => $result['user_wallet'] -> $field,
			'create_time' => time(),
		];

		switch($data['active']) {
			case 1:
				$result['user_wallet'] -> $field += $data['amount'];
				$dataLog['new_amount'] = $result['user_wallet'] -> $field;
				break;
			case 2:
				if($data['amount'] > $result['user_wallet'] -> $field) {
					switch($data['type']) {
						case 1:
						case '1':
							return ['code' => -1, 'message' => '零售积分数量不足'];
						case 2:
						case '2':
							return ['code' => -1, 'message' => '业务积分数量不足'];
						case 3:
						case '3':
							return ['code' => -1, 'message' => '商城积分数量不足'];
						case 4:
						case '4':
							return ['code' => -1, 'message' => '交易票数量不足'];
						case 5:
						case '5':
							return ['code' => -1, 'message' => '开户票数量不足'];
						default:
							return ['code' => -1, 'message' => '数量不足'];
					}
				}

				$result['user_wallet'] -> $field -= $data['amount'];
				$dataLog['new_amount'] = $result['user_wallet'] -> $field;
				break;
			default:
				return ['code' => -1, 'message' => '操作设置有误'];
		}

		if(!$result['user_wallet'] -> save()) {
			return ['code' => -1, 'message' => '操作失败'];
		}

		$classNameLog = __NAMESPACE__ . '\\Log\\Model';
		if(!(new $classNameLog($dataLog)) -> create()) {
			return ['code' => -1, 'message' => '操作失败'];
		}

		return ['code' => 0, 'message' => '操作成功'];
	}

	/**
	 * 加
	 */
	public function updateIncAmount($data) {
		$data['active'] = 1;
		return $this -> updateAmount($data);
	}

	/**
	 * 减
	 */
	public function updateDecAmount($data) {
		$data['active'] = 2;
		return $this -> updateAmount($data);
	}

	public function createTransferLog($data) {
		$data['type'] = 0;
		$data['create_time'] = time();
		$className = __NAMESPACE__ . '\\Transfer\\Model';
		if(!(new $className($data)) -> create()) {
			return ['code' => -1, 'message' => '操作失败'];
		}

		return ['code' => 0, 'message' => '操作成功'];
	}

	/**
	 * 转账
	 */
	public function transfer($data) {
		$serviceF = $this -> updateDecAmount(['type'=>$data['field'],'amount'=>$data['amount'],'user_id'=>$data['from_user_id']]);
		if($serviceF['code'] != 0) {
			return $serviceF;
		}

		$serviceS = $this -> updateIncAmount(['type'=>$data['field'],'amount'=>$data['amount'],'user_id'=>$data['to_user_id']]);
		if($serviceS['code'] != 0) {
			return $serviceS;
		}

		$serviceT = $this -> createTransferLog($data);
		if($serviceT['code'] != 0) {
			return $serviceT;
		}

		return ['code'=>0];
	}
}
